package com.jie.component;

import com.jie.enums.BizCodeEnum;
import com.jie.service.MailService;
import com.jie.utils.CheckUtil;
import com.jie.utils.CommonUtil;
import com.jie.utils.JsonData;
import com.jie.vo.EmailVO;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
@Slf4j
@Component
public class MailReceiver {

    private static String SUBJECT="商城邮箱验证码邮件";
    private static String CONTENT="欢迎你的验证码是%s,有效时间为60秒";

    @Autowired
    private MailService mailService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 监听mail端口 发送邮箱验证码
     * @param emailVO
     */
    @RabbitListener(queues = "mail.QQ")
    public void handler(EmailVO emailVO) {
        if(CheckUtil.isEmail(emailVO.getTo())){
            //邮箱验证码
            mailService.sendSimpleMail(emailVO.getTo(),SUBJECT,String.format(CONTENT, emailVO.getRandomCode()));
            log.info("发送邮箱成功{}",emailVO.getRandomCode());
        }else if(CheckUtil.isPhone(emailVO.getTo())){
            //TODO 短信验证码
        }
    }
}